#include "TcpServer.h"

TcpServer::TcpServer(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

	this->setWindowIcon(QIcon(":/images/images/Qicon.jpg"));
	this->setWindowTitle("Tcp服务端");

	this->server = new QTcpServer;
	this->server->listen(QHostAddress::AnyIPv4, 8000);
	QObject::connect(server, &QTcpServer::newConnection, this, &TcpServer::newClientHandler);
}

TcpServer::~TcpServer()
{
}

void TcpServer::newClientHandler()
{
	QTcpSocket *socket = this->server->nextPendingConnection();

	ui.ipLineEdit->setText(socket->peerAddress().toString());
	ui.portLineEdit->setText(QString::number(socket->peerPort()));
	//启动线程
	myThread *thread = new myThread(socket);
	QObject::connect(thread, &myThread::sendToWidget, this, &TcpServer::receiveSlot);
	thread->start();

}

void TcpServer::clientInfoSlot()
{
	QTcpSocket *socket = (QTcpSocket *)sender();
	ui.mainLineEdit->setText(QString(socket->readAll()));
}

void TcpServer::receiveSlot(QString str)
{
	ui.mainLineEdit->setText(str);
}
